package org.xbill.DNS;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.xbill.DNS.utils.base16;

public class OPTRecord extends Record
{
  private static final long serialVersionUID = 2080115326L;
  private List options;

  public OPTRecord(int paramInt1, int paramInt2, int paramInt3)
  {
    this(paramInt1, paramInt2, paramInt3, 0, null);
  }

  public OPTRecord(int paramInt1, int paramInt2, int paramInt3, int paramInt4)
  {
    this(paramInt1, paramInt2, paramInt3, paramInt4, null);
  }

  public OPTRecord(int paramInt1, int paramInt2, int paramInt3, int paramInt4, List paramList)
  {
    super(Name.root, 41, paramInt1, 0L);
    checkU16("payloadSize", paramInt1);
    checkU8("xrcode", paramInt2);
    checkU8("version", paramInt3);
    checkU16("flags", paramInt4);
    this.ttl = ((paramInt2 << 24) + (paramInt3 << 16) + paramInt4);
    if (paramList != null)
      this.options = new ArrayList(paramList);
  }

  public int getExtendedRcode()
  {
    return (int)(this.ttl >>> 24);
  }

  public int getFlags()
  {
    return (int)(0xFFFF & this.ttl);
  }

  Record getObject()
  {
    return new OPTRecord();
  }

  public List getOptions()
  {
    List localList;
    if (this.options == null)
      localList = Collections.EMPTY_LIST;
    while (true)
    {
      return localList;
      localList = Collections.unmodifiableList(this.options);
    }
  }

  public List getOptions(int paramInt)
  {
    Object localObject;
    if (this.options == null)
      localObject = Collections.EMPTY_LIST;
    while (true)
    {
      return localObject;
      ArrayList localArrayList = null;
      Iterator localIterator = this.options.iterator();
      while (true)
      {
        Option localOption;
        do
        {
          while (!(localIterator.hasNext()))
          {
            if (localArrayList != null)
              break label95;
            localObject = Collections.EMPTY_LIST;
          }
          localOption = (Option)localIterator.next();
        }
        while (localOption.code != paramInt);
        if (localArrayList == null)
          localArrayList = new ArrayList();
        localArrayList.add(localOption.data);
      }
      label95: localObject = localArrayList;
    }
  }

  public int getPayloadSize()
  {
    return this.dclass;
  }

  public int getVersion()
  {
    return (int)(0xFF & this.ttl >>> 16);
  }

  void rdataFromString(Tokenizer paramTokenizer, Name paramName)
    throws IOException
  {
    throw paramTokenizer.exception("no text format defined for OPT");
  }

  void rrFromWire(DNSInput paramDNSInput)
    throws IOException
  {
    if (paramDNSInput.remaining() > 0)
      this.options = new ArrayList();
    while (true)
    {
      if (paramDNSInput.remaining() <= 0)
        return;
      int i = paramDNSInput.readU16();
      byte[] arrayOfByte = paramDNSInput.readByteArray(paramDNSInput.readU16());
      this.options.add(new Option(i, arrayOfByte));
    }
  }

  String rrToString()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    if (this.options != null)
    {
      localStringBuffer.append(this.options);
      localStringBuffer.append(" ");
    }
    localStringBuffer.append(" ; payload ");
    localStringBuffer.append(getPayloadSize());
    localStringBuffer.append(", xrcode ");
    localStringBuffer.append(getExtendedRcode());
    localStringBuffer.append(", version ");
    localStringBuffer.append(getVersion());
    localStringBuffer.append(", flags ");
    localStringBuffer.append(getFlags());
    return localStringBuffer.toString();
  }

  void rrToWire(DNSOutput paramDNSOutput, Compression paramCompression, boolean paramBoolean)
  {
    if (this.options == null)
      return;
    Iterator localIterator = this.options.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()));
      Option localOption = (Option)localIterator.next();
      paramDNSOutput.writeU16(localOption.code);
      paramDNSOutput.writeU16(localOption.data.length);
      paramDNSOutput.writeByteArray(localOption.data);
    }
  }

  public static class Option
  {
    public final int code;
    public final byte[] data;

    public Option(int paramInt, byte[] paramArrayOfByte)
    {
      this.code = OPTRecord.checkU8("option code", paramInt);
      this.data = paramArrayOfByte;
    }

    public String toString()
    {
      return "{" + this.code + " <" + base16.toString(this.data) + ">}";
    }
  }
}